package adfc2.actions;

import adfc2.core.Creature;
import adfc2.core.Item;
import adfc2.core.Reaction;

/**
 * Выполнить реакцию
 * TODO if workshop = null
 */
public class ReactionAction extends LongAction {

    public final Reaction reaction;

    public ReactionAction(Creature actor, Reaction reaction) {
        super(actor);
        this.reaction = reaction;
    }

    @Override
    protected void started() {
        assert actor.container == reaction.workshop;
        assert _a_reactionItemsInWorkshop();
        assert reaction.valid();
    }

    private boolean _a_reactionItemsInWorkshop() {
        for (Item item : reaction.input) {
            assert item.owner == reaction.workshop;
        }
        return true;
    }

    @Override
    protected boolean progressing(long dt) {
        return (totaltime > craftTime());
    }

    private long craftTime() {
        return 1000;
    }

    @Override
    protected void finished() {
        Item[] output = reaction.output();
        for (Item item : reaction.input) {
            reaction.workshop.removeItem(item);
        }
        for (Item item : output) {
            reaction.workshop.addItem(item);
        }
    }

    @Override
    protected void interrupted() {
    }

    @Override
    public double estimateProgress() {
        return ((double) totaltime) / craftTime();
    }
}
